<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 订单ajax类
 * @package  ajax/backend/bill
 * @author   allen.qiang
 */

quanli_require('module/base/bill/billTemp.class.php');
quanli_require('app_class/goodsHandler.class.php');
quanli_require('module/base/system/sysUser.class.php');

class ajax_billTemp {

    public function execute()
    {
        if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg']))
        {
            switch($_REQUEST['operFlg']) {

                case 1: //查询列表数据
                    $this->searchBillTemp();
                    break;

                case 2: //修改状态
                    $this->chanageStatus();
                    break;

                case 3: //查询商品
                    $this->searchGoods();
                    break;

                case 4: //保存小样订单
                    $this->saveTemp();
                    break;

                case 5: //小样订单发货
                    $this->sendTemp();
                    break;

                case 6: //查询小样订单历史数据
                    $this->searchHisData();
                    break;

                default:
                    returnJsonData('参数错误');
                    break;
            }
        } else {
            returnJsonData('参数错误');
        }
    }

    /**
     * 查询小样订单
     */
    private function searchBillTemp ()
    {
        global $G_PGLOB;

        $billTemp = new billTemp( $G_PGLOB->DBCA );

        $res_arr = $billTemp->searchBillTemp( $_REQUEST );

        //组织返回的数据
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ($res_arr) {

            foreach($res_arr as &$temp)
            {
                $statusName = '';
                switch ($temp->tempStatus){
                    case 0: $statusName = '创建'; break;
                    case 1: $statusName = '审核'; break;
                    case 2: $statusName = '制作中'; break;
                    case 3: $statusName = '制作完成'; break;
                    case 4: $statusName = '已发货'; break;
                    case 5: $statusName = '已收货'; break;
                    case 9: $statusName = '取消'; break;
                }

                $operHtml = '<a href="javascript:void(0);" onclick="temp.detail(\''.$temp->tempCode.'\')">详情</a>&nbsp;&nbsp;';
                if($temp->tempStatus == 0){
                    $operHtml .= '<a href="javascript:void(0);" onclick="temp.audit(\''.$temp->tempCode.'\')">审核</a>&nbsp;&nbsp;';
                    $operHtml .= '<a href="javascript:void(0);" onclick="temp.cancel(\''.$temp->tempCode.'\')">取消</a>&nbsp;';
                }
                $result_array['rows'][] = array(
                    'id' => $temp->costCode,
                    'cell' => array(
                        $operHtml,
                        $statusName,
                        $temp->tempCode,
                        $temp->tempName,
                        $temp->company_name,
                        $temp->area,
                        $temp->categoryName,
                        $temp->requestDate,
                        $temp->distribution_technician_name,
                        $temp->createTime
                    )
                );
            }
        }
        echo json_encode($result_array);
    }

    //修改状态
    public function chanageStatus(){
        global $G_PGLOB;
        $billTemp = new billTemp( $G_PGLOB->DBCA );
        $param = array( 'tempCode' => $_REQUEST['code'], 'status' => $_REQUEST['status']);
        $param['operator'] = $G_PGLOB->currentUserID;
        $res = $billTemp->changeStatus($param);
        if($res && $res['code'] == 200){
            if($_REQUEST['status'] == 1){
                try{
                    quanli_include('module/base/company/companyMaster.class.php');
                    $company = new companyMaster();
                    $arg = array('company_id' => $res['data']->companyId);
                    $compay_result = $company->search( $arg );
                    if($compay_result && isset($compay_result[0]) && !empty($compay_result[0]['wx_openid'])){
                        quanli_include('framework/wx/wxHelp.php');
                        $params = array(
                            'openId' => $compay_result[0]['wx_openid'],
                            'url' => G_SHOP_CDN . '/wap/orderDtl.html?ajaxFlg=2&code='.$_REQUEST['code'],
                            'title' => '小样订单提醒',
                            'code' => $_REQUEST['code'],
                            'date' => date('Y-m-d'),
                            'remark' => '您的小样订单已经审核通过，点击查看详情'
                        );
                        wxHelp::wxMessage($params);
                        /*推送给对应客户的销售员微信账号*/
                        global $G_SHOP;
                        $param = array('userID' => $compay_result[0]['userID'], 'useFlg' => 1);
                        $user_master = new sysUser($G_SHOP->DBCA);//实例用户类
                        $temp_user = $user_master->searchUserInfo($param);
                        if ($temp_user[0]['isSalesman'] == '1' && $temp_user[0]['wx_openid'] != ''){
                            $params = array(
                                'openId' => $temp_user[0]['wx_openid'],
                                'url' => G_SHOP_CDN . '/wap/orderDtl.html?ajaxFlg=2&code='.$_REQUEST['code'],
                                'title' => '小样订单提醒',
                                'code' => $_REQUEST['code'],
                                'date' => date('Y-m-d'),
                                'remark' => '大客户' . $compay_result[0]['company_name'] . '的小样订单已经审核通过，点击查看详情'
                            );
                            wxHelp::wxMessage($params);
                        }
                    }
                }catch (Exception $e){ elog($e->getMessage()); }

                $msg = '审核成功';
            }else{
                $msg = '取消成功';
            }
            returnJsonData($msg, 200);
        }
        returnJsonData('操作失败', 400);
    }

    public function searchGoods(){
        global $G_PGLOB;
        $goodsHandler = new goodsHandler( $G_PGLOB->DBCA ) ;
        $params = array('forsale' => 1);
        if(isset($_REQUEST['categoryId'])){
            $params['bigCategoryID'] = $_REQUEST['categoryId'];
        }
        if(isset($_REQUEST['goodsCode'])){
            $params['postID'] = $_REQUEST['goodsCode'];
        }
        $result_data  = $goodsHandler->searchGoodsSale( $params ) ;

        if(isset($_REQUEST['goodsCode'])) {
            $temp_sync = array();
            if($result_data['syncID']){
                $syncID = explode('|',$result_data['syncID']);
                $syncopate = explode('|',$result_data['syncopate']);
                $temp_sync = array_combine($syncID, $syncopate);
            }
            $temp_birth = array();
            if($result_data['birthID']){
                $birthID = explode('|',$result_data['birthID']);
                $birthcard = explode('|',$result_data['birthcard']);
                $temp_birth = array_combine($birthID, $birthcard);
            }

            $result_data['syncArr'] = $temp_sync;
            $result_data['birthArr'] = $temp_birth;
        }
        returnJsonData('', 200, $result_data);
    }

    //保存小样订单
    public function saveTemp(){
        if(!isset($_REQUEST['companyId'],$_REQUEST['companyName'],$_REQUEST['tempName'],$_REQUEST['area'],$_REQUEST['categoryName'],$_REQUEST['expressId'],$_REQUEST['dtl']) || !is_array($_REQUEST['dtl'])) {
            returnJsonData('缺少参数', 400);
        }

        global $G_PGLOB;

        $billCost = new billTemp( $G_PGLOB->DBCA );
        $_REQUEST['operator'] = $G_PGLOB->currentUserID;
        $res_arr = $billCost->saveTemp( $_REQUEST );
        if($res_arr['code'] == 200){
            returnJsonData('保存成功', 200);
        }
        returnJsonData('保存失败', 400);
    }

    //小样订单发货
    public function sendTemp(){
        if(!isset($_REQUEST['code'],$_REQUEST['expressNum']) || empty($_REQUEST['code']) || empty($_REQUEST['expressNum'])) {
            returnJsonData('缺少参数', 400);
        }

        global $G_PGLOB;
        $billTemp = new billTemp( $G_PGLOB->DBCA );
        $param = array(
            'tempCode' => $_REQUEST['code'],
            'sender' => $G_PGLOB->currentUserID,
            'expressNum' => $_REQUEST['expressNum'],
            'status' => 4
        );
        $res = $billTemp->changeStatus($param);
        try{
            quanli_include('module/base/company/companyMaster.class.php');
            $company = new companyMaster();
            $arg = array('company_id' => $res['data']->companyId);
            $compay_result = $company->search( $arg );
            if($compay_result && isset($compay_result[0]) && !empty($compay_result[0]['wx_openid'])){
                quanli_include('framework/wx/wxHelp.php');
                $params = array(
                    'openId' => $compay_result[0]['wx_openid'],
                    'url' => G_SHOP_CDN . '/wap/orderDtl.html?ajaxFlg=2&code='.$_REQUEST['code'],
                    'title' => '小样订单提醒',
                    'code' => $_REQUEST['code'],
                    'date' => date('Y-m-d'),
                    'remark' => '您的小样订单已经发货，点击查看详情'
                );
                wxHelp::wxMessage($params);
                /*推送给对应客户的销售员微信账号*/
                global $G_SHOP;
                $param = array('userID' => $compay_result[0]['userID'], 'useFlg' => 1);
                $user_master = new sysUser($G_SHOP->DBCA);//实例用户类
                $temp_user = $user_master->searchUserInfo($param);
                if ($temp_user[0]['isSalesman'] == '1' && $temp_user[0]['wx_openid'] != ''){
                    $params = array(
                        'openId' => $temp_user[0]['wx_openid'],
                        'url' => G_SHOP_CDN . '/wap/orderDtl.html?ajaxFlg=2&code='.$_REQUEST['code'],
                        'title' => '小样订单提醒',
                        'code' => $_REQUEST['code'],
                        'date' => date('Y-m-d'),
                        'remark' => '大客户' . $compay_result[0]['company_name'] . '的小样订单已经发货，点击查看详情'
                    );
                    wxHelp::wxMessage($params);
                }
            }
        }catch (Exception $e){ elog($e->getMessage()); }

        if($res && $res['code'] == 200){
            returnJsonData('发货成功', 200);
        }
        returnJsonData('发货失败', 400);
    }

    function searchHisData(){
        if(!isset($_REQUEST['companyId'],$_REQUEST['categoryId'])) {
            returnJsonData('缺少参数', 400);
        }
        global $G_PGLOB;
        $billTemp = new billTemp( $G_PGLOB->DBCA );
        $res_arr = $billTemp->searchHisData( $_REQUEST );
        $data = array();
        foreach ($res_arr as $temp){
            $data[] = array(
                'tempCode' => $temp->tempCode,
                'tempName' => $temp->tempName,
                'goodsId' => $temp->goodsId,
                'categoryName' => $temp->categoryName,
                'color' => $temp->color,
                'colorDis' => $temp->colorDis,
                'amount' => $temp->amount,
                'effect' => $temp->effect,
                'spec' => $temp->spec,
                'remark' => $temp->remark,
            );
        }
        returnJsonData('', 200, $data);

    }

}
